#include <stdio.h>
#include <stdlib.h>
#include <csp_spu.h>
#include <csp_debug.h>
#include "../../shared/sor.h"



int global_pid; 


#define CBASE_IN  0x6000 
#define CBASE_OUT 0x8000

int Run(){
	int i;
	int pid = csp_pid();


	global_pid = csp_pid();

	csp_channel init_o2a = 
		csp_create_channel(ONE2ANY_CHANNEL, INIT_CHANNEL, sizeof(init_data_t));

	init_data_t init[N_WORKERS] __attribute__ ((aligned(128)));

	init[0].cTopIn     = 0; 
	init[0].cTopOut    = 0;
	init[0].stribe     = 0;
	init[0].color      = 0;
	init[0].cBottomIn  = CBASE_IN;
	init[0].cBottomOut = CBASE_OUT; 
	init[0].width	   = WIDTH;
	init[0].height 	   = HEIGHT / N_WORKERS;

	//csp_channel_write(init_o2a, &init[0]);

	for(i=1; i < N_WORKERS; i++){
		init[i].cTopIn     = init[i-1].cBottomOut;  
		init[i].cTopOut    = init[i-1].cBottomIn;
		init[i].cBottomIn  = CBASE_IN  + i + 1;
		init[i].cBottomOut = CBASE_OUT + i + 1;
		init[i].stribe = i;
		if( N_WORKERS-1 == i ){
			init[i].cBottomIn=0;
			init[i].cBottomOut=0; 
		}
		init[i].color     = i % 2;
		init[i].width  = WIDTH;
		init[i].height = HEIGHT / N_WORKERS;


		//csp_channel_write(init_o2a, &init[i]);

//		init[i].cTopIn     = init.cBottomOut;  
//		init[i].cTopOut    = init.cBottomIn;
//		init[i].cBottomIn  = CBASE_IN  + i + 1;
//		init[i].cBottomOut = CBASE_OUT + i + 1;
	}

//	for(i=0; i < N_WORKERS; i+=2){csp_channel_write(init_o2a, &init[i]);}
//	for(i=1; i < N_WORKERS; i+=2){csp_channel_write(init_o2a, &init[i]);}

	ASSERT( pid == csp_pid() );
	ASSERT( global_pid == csp_pid() );

	printf("sor_init exit\n");

	return 0;
}
